﻿using System;
using System.Collections;
using System.Collections.Generic;
using Gameplay.PVE;
using UnityEngine;
using Object = UnityEngine.Object;

public class RpgBattleSceneObstacle : MonoBehaviour
{
    //public List<MeshFilter> obstacles = new List<MeshFilter>();
    public List<Vector2> blocks = new List<Vector2>();
    
    public List<Vector3> GetObstacleBlocks(Vector3 astarCenter)
    {
        //obstacles.Clear();
        var list = new List<Vector3>();
        var meshFilters = this.GetComponentsInChildren<MeshFilter>();
        for (int i = 0; i < meshFilters.Length; i++)
        {
            //obstacles.Add(meshFilters[i]);
            var blocks = GetBlocksByMesh(meshFilters[i],astarCenter);
            list.AddRange(blocks);
        }
        return list;
    }

    private List<Vector3> GetBlocksByMesh(MeshFilter meshFilter,Vector3 astarCenter)
    {
        var list = new List<Vector3>();
        int layer = LayerMask.NameToLayer("PveMap");
        var gameObject = meshFilter.gameObject;
        var originLayer = gameObject.layer;
        var collider = gameObject.GetOrAddComponent<BoxCollider>();
        collider.enabled = true;
        gameObject.SetLayerRecursively(layer);
        var objTrans = gameObject.transform;
        //找到最左最右最上最下的点的位置
        var xMin = float.MaxValue;
        var xMax = float.MinValue;
        var zMin = float.MaxValue;
        var zMax = float.MinValue;
        var mesh = meshFilter.sharedMesh;
        for (int i = 0; i < mesh.vertexCount; i++)
        {
            var v = mesh.vertices[i];
            var point = objTrans.TransformPoint(v);
            xMin = Mathf.Min(xMin, point.x);
            xMax = Mathf.Max(xMax, point.x);
            zMin = Mathf.Min(zMin, point.z);
            zMax = Mathf.Max(zMax, point.z);
        }

        xMin = Mathf.FloorToInt((xMin - astarCenter.x) / AStarMgr.gridSize.x);
        xMax = Mathf.CeilToInt((xMax - astarCenter.x) / AStarMgr.gridSize.x);
        zMin = Mathf.FloorToInt((zMin - astarCenter.z) / AStarMgr.gridSize.y);
        zMax = Mathf.CeilToInt((zMax - astarCenter.z) / AStarMgr.gridSize.y);
        
        for (int i = (int)xMin; i <= (int)xMax; i++)
        {
            for (int j = (int)zMin; j <= (int)zMax; j ++)
            {
                var testList = new List<Vector3>();
                var gridX = AStarMgr.gridSize.x;
                var gridZ = AStarMgr.gridSize.y;
                var testPoint = new Vector3(i * gridX, 1, j * gridZ) + astarCenter;
                testList.Add(testPoint);
                testList.Add(testPoint + new Vector3(gridX / 2,0,gridZ / 2));
                testList.Add(testPoint + new Vector3(-gridX / 2,0,gridZ / 2));
                testList.Add(testPoint + new Vector3(-gridX / 2,0,-gridZ / 2));
                testList.Add(testPoint + new Vector3(gridX / 2,0,-gridZ / 2));
                bool hasBlock = false;
                for (int k = 0; k < testList.Count; k++)
                {
                    Ray ray = new Ray(testList[k],Vector3.down);
                    if (Physics.Raycast(ray, 2, 1 << layer))
                    {
                        Debug.LogError(testList[k]);
                        //list.Add(new Vector3(testList[k].x,0,testList[k].z));
                        hasBlock = true;
                        break;
                    }
                }

                if (hasBlock)
                {
                    list.Add(new Vector3(testPoint.x,0,testPoint.z));
                }
            }
        }
        gameObject.SetLayerRecursively(originLayer);
        Object.DestroyImmediate(collider);
        return list;
    }
}
